var app = getApp(),
  loading = 2;
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
Page({
  data: {
    page: 1,
    hotel: [],
    timg: !1,
    recomed: 2,
    day1: '',
    day2: '',
    keywordsValue:''
  },
  onLoad: function(t) {
    console.log(t), wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
    var e = this;
    console.log(t.keywords == 'null' ) ;
    e.setData({
      keywordsValue: t.keywords == null && t.keywords == '' && t.keywords == undefined ? '' : t.keywords,
      day1: wx.getStorageSync("day1").slice(5, 10),
      day2: wx.getStorageSync("day2").slice(5, 10)
    })
    e.setData({
      color: wx.getStorageSync("platform").color
    }), e.setData({
      start: app.util.time(),
      end: app.util.addDate(app.util.time(), 28)
    }), 1 == t.nearby ? e.setData({
      nearby: t.nearby,
      Recommend: 2,
      price_sorting: 0
    }) : e.setData({
      nearby: 0,
      Recommend: 1,
      price_sorting: 0
    });
    var a = wx.getStorageSync("platform"),
      o = wx.getStorageSync("url");
    e.setData({
      platform: a,
      url: o,
      lat1: wx.getStorageSync("latitude"),
      lng1: wx.getStorageSync("longitude")
    }), 2 == location && wx.showLoading({
      title: "正在加载",
      complete: function() {},
      fail: function() {}
    }), e.refresh(), e.date();
  },
  date: function(t) {
    var e = wx.getStorageSync("day1"),
      a = wx.getStorageSync("day2"),
      o = wx.getStorageSync("day"),
      r = app.util.time();
    if (console.log(n), wx.setStorageSync("datein", n), "" == e) {
      var n = app.util.time();
      wx.setStorageSync("datein", n);
    } else if (e < r) n = r;
    else n = e;
    if ("" == a) var i = app.util.addDate(r, 1);
    else {
      var s = app.util.addDate(r, 1);
      if (a < s) i = s;
      else i = a;
    }
    o = app.util.day(i, n);
    wx.setStorageSync("day1", n), wx.setStorageSync("day2", i), wx.setStorageSync("day", o),
      this.setData({
        datein: n,
        dateout: i,
        time: o,
        current_date: n
      });
  },
  refresh: function(t) {
    var s = this,
      e = wx.getStorageSync("url");
    s.setData({
      url: e
    });
    console.log(wx.getStorageSync('day1'));
    var c = s.data.page,
      l = s.data.hotel,
      g = s.data.Recommend,
      d = s.data.price_sorting,
      p = s.data.nearby;
    app.util.request({
      url: "entry/wxapp/JdList",
      cachetime: "0",
      data: {
        page: c,
        size: 7,
        time: wx.getStorageSync('day1'),
        keywords: this.data.keywordsValue != '' && this.data.keywordsValue != null ? this.data.keywordsValue:''
      },
      success: function(t) {
        // console.log(t);
        if (console.log(t), 0 < t.data.length) {
          s.setData({
            page: c + 1,
            none_more: !1
          }), l = l.concat(t.data);
          console.log(s.data.lat1);
          console.log(s.data.lng1);
          for (var e = 0; e < l.length; e++) {
            var a = l[e].coordinates.split(",");
            l[e].lat2 = a[0], l[e].lng2 = a[1];
            var o = s.data.lat1,
              r = s.data.lng1,
              n = Number(a[0]),
              i = Number(a[1]);
            l[e].distance = app.util.location(o, n, r, i);
          }
          1 == g ? s.setData({
            hotel: l,
            timg: !0
          }) : (1 == d && s.setData({
            hotel: l.sort(app.sort_price_order),
            timg: !0
          }), 2 == d && s.setData({
            hotel: l.sort(app.sort_price_Reverse),
            timg: !0
          }), 1 == p && s.setData({
            hotel: l.sort(app.sort_distance_order),
            timg: !0
          }), 2 == p && s.setData({
            hotel: l.sort(app.sort_distance_Reverse),
            timg: !0
          }));
        } else s.setData({
          none_more: !0,
          timg: !0
        });
      }
    });
  },
  recomed: function() {
    var e = this;
    e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      recomed: 1
    });
    app.util.request({
      url: "entry/wxapp/ScoreSort",
      cachetime: "0",
      data: {
        i: base.acid,
        c: 'entry',
        a: 'wxapp',
        m: 'zh_jdgjb',
        size: 7,
        time: wx.getStorageSync('day1'),
        keywords: e.data.keywordsValue != null ? e.data.keywordsValue:''
      },
      success: function(t) {
        var o = e.data.lat1,
          r = e.data.lng1;
        t.data.forEach(function(item, index) {
          console.log(index);
          var a = item.coordinates.split(",");
          t.data[index].lat2 = a[0];
          t.data[index].lng2 = a[1];
          t.data[index].distance = app.util.location(o, Number(a[0]), r, Number(a[1]));
        })
        e.setData({
          hotel: t.data
        });
      }
    })
  },
  Recommend: function(t) {
    var e = this;
    e.data.Recommend;
    e.setData({
      price_sorting: 0,
      Recommend: 1,
      nearby: 0,
      page: 1,
      hotel: [],
      recomed: 2
    }), e.refresh();
  },
  price_sorting: function(t) {
    var e = this,
      a = e.data.price_sorting;
    e.data.hotel;
    0 == a ? (e.setData({
      price_sorting: 2,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1,
      recomed: 2
    }), e.refresh()) : 1 == a ? (e.setData({
      price_sorting: 2,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh()) : 2 == a && (e.setData({
      price_sorting: 1,
      Recommend: 2,
      nearby: 0,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh());
  },
  nearby: function(t) {
    var e = this,
      a = e.data.nearby;
    e.data.hotel_b;
    0 == a ? (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 2,
      page: 1,
      hotel: [],
      timg: !1,
      recomed: 2
    }), e.refresh()) : 1 == a ? (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 2,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh()) : 2 == a && (e.setData({
      price_sorting: 0,
      Recommend: 2,
      nearby: 1,
      page: 1,
      hotel: [],
      timg: !1
    }), e.refresh());
  },
  bindDateChange1: function(t) {
    console.log("picker发送选择改变，携带值为", t.detail.value);
    var e = t.detail.value,
      a = this.data.dateout,
      o = (this.data.current_date, app.getTime2Time(a, e));
    wx.setStorageSync("day1", e), wx.setStorageSync("day2", a), wx.setStorageSync("day", o),
      this.setData({
        datein: t.detail.value,
        time: o
      });
  },
  bindDateChange2: function(t) {
    console.log("picker发送选择改变，携带值为", t.detail.value);
    var e = this.data.datein,
      a = t.detail.value;
    console.log(e + "+" + a);
    var o = app.getTime2Time(a, e);
    wx.setStorageSync("day1", e), wx.setStorageSync("day2", a), wx.setStorageSync("day", o),
      this.setData({
        dateout: t.detail.value,
        time: o
      });
  },
  conlist: function(t) {
    console.log(t), wx.navigateTo({
      url: "hotel_info?hotel_id=" + t.currentTarget.dataset.id
    });
  },
  search: function(t) {
    wx.redirectTo({
      url: "../index/search?whereFrom=1"
    });
  },
  onReady: function() {},
  onShow: function() {
    console.log("页面显示"), this.date();
  },
  onHide: function() {},
  onUnload: function() {
    console.log('TM卸载了');
    this.setData({
      location: 2
    });
  },
  onPageScroll:function(e){
    console.log(e);
  },
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      hotel: [],
      nearby: 0,
      Recommend: 1,
      price_sorting: 0
    }), this.refresh(), wx, wx.stopPullDownRefresh();
  },
  onReachBottom: function() {
    console.log('TM到底了');
    // this.setData({
    //   location: 2
    // });
    this.setData({
      timg: !1
    }), this.refresh();
  }
});